
import AbsMenu from '../absmenu.js'
import { ElMessageBox } from 'element-plus';
class ExportMenu extends AbsMenu.AbsMenu {
    constructor(map, item) {
        super(map, item);
    }

    act2Click() {
        console.log("export-----+" + this.menuSubItem);
        switch (this.menuSubItem) {
            case 'png':
                this.exportDefaultPng();
                break;

            case '4kpng':
                this.export4kPng();
                break;
        }
    }

    exportDefaultPng() {
        let width = 0; // 表示使用画布尺寸
        let height = 0;
        let that = this;
        ElMessageBox.confirm('是否带背景导出？', '提示', {
            confirmButtonText: '是',
            cancelButtonText: '否',
            type: 'warning',
        }).then(() => {
            that.exportPng(width, height, true)
        }).catch(() => {
            that.exportPng(width, height, false)
        })
    }

    export4kPng() {
        console.log("exportPng------------------");
        let scale = 1;
        let width = 14000;
        let height = 13000;
        let rect = this.map.m_pGetDataINTF.getMapValidSize();
        let w = Math.ceil(rect.width);
        let h = Math.ceil(rect.height);
        let defaultText = w*scale + ',' + h*scale;
        let that = this;
        ElMessageBox.prompt('请输入尺寸(长,宽)', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            inputType: 'text',
            inputPattern: /^\d+,\d+$/,
            inputErrorMessage: '图片尺寸不合法，仅允许输入数字和逗号',
            inputValue: defaultText // 设置默认值
        }).then(({ value }) => {
            let text = value;
            text = text.trim();
            text = text.toUpperCase();
            let list = text.split(',');
            if (list.length == 2) {
                width = parseInt(list[0]);
                height = parseInt(list[1]);

                ElMessageBox.confirm('是否带背景导出？', '提示', {
                    confirmButtonText: '是',
                    cancelButtonText: '否',
                    type: 'warning',
                }).then(() => {
                    that.exportPng(width, height, true)
                }).catch(() => {
                    that.exportPng(width, height, false)
                })
            }
            else {
                return;
            }
        }).catch(() => {
            console.log('取消输入');
            return;
        });


    }

    exportPng(width, height, exportBackground) {
        console.log("exportPng------------------");
        let exportCanvas = null;
        let useDefaultMap = false;
        if (width == 0 || height == 0) {
            exportCanvas = this.map.getCanvas();
            useDefaultMap = true;
        }
        else {
            exportCanvas = document.createElement("canvas");
            exportCanvas.setAttribute("width", width);
            exportCanvas.setAttribute("height", height);
        }

        this.map.exportPng(exportCanvas, width, height, exportBackground);

        let imagedata = exportCanvas.toDataURL("image/png", 0.5);

        var oA = document.createElement("a");
        let curentName = window.gstore.state.curentFile["name"];
        if (!curentName || (curentName && curentName.length == 0)) {
            curentName = "unnamed";
        }
        oA.download = curentName + '.png';// 设置下载的文件名，默认是'下载'
        oA.href = imagedata;
        document.body.appendChild(oA);
        oA.click();
        oA.remove(); // 下载之后把创建的元素删除
        if (!useDefaultMap) {
            exportCanvas.remove(); // 下载之后把创建的元素删除
        }
        this.map.rePaint(1);
    }
}

export default { ExportMenu }

